<template>
  <div class="app-container drag-container">
    <el-row :gutter="0">
      <!--   树形的菜单，现在这里只有静态数据（后期如果相加动态的可以绑上数据）
      <el-col :xs="24" :sm="6" :md="5" :lg="4" class="drag-area" id="drag-area">
        <div style="text-align: center; color: #0fa8e7">数据选择</div>

        <el-tree
          :data="data"
          :props="defaultProps"
          @node-click="handleNodeClick"
        ></el-tree>
        <div class="drag-tip" id="drag-tip"></div>
      </el-col>
      <el-col :xs="24" :sm="18" :md="18.7" :lg="20" id="drag-content"> -->

      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        v-show="showSearch"
        label-width="100px"
      >
        <!-- <el-form-item label="学年名称" >
            <el-select
              v-model="queryParams.schoolYearId"
              placeholder="请选择学年"
              clearable
              @change="searchTermByYear()"
              size="small"
              style="width: 240px"
            >
              <el-option
                v-for="item in yearOptions"
                :key="item.index"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="学期名称" >
            <el-select
              v-model="queryParams.termId"
              placeholder="请选择学期"
              clearable
              size="small"
              style="width: 240px"
              @change="searchCollegeByTerm()"
            >
              <el-option
                v-for="item in termNameOptions"
                :key="item.index"
                :label="item.termName"
                :value="item.id"
              />
            </el-select>
          </el-form-item>

          <el-form-item label="学院名称" >
            <el-select
              v-model="queryParams.collegeId"
              placeholder="请选择学院"
              clearable
              size="small"
              style="width: 240px"
              @change="searchCourseByCollege()"
            >
              <el-option
                v-for="item in collegeOptions"
                :key="item.index"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item> -->
        <!-- <el-form-item label="开课单位" prop="startingUnit">
          <el-select
            v-model="queryParams.startingUnit"
            placeholder="请选择学院"
            clearable
            filterable
            size="small"
            style="width: 100%"
          >
            <el-option
              v-for="item in coledoptions"
              :key="item.index"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item> -->
        <el-form-item label="题目名称" prop="courseName">
          <el-input
            v-model="queryParams.courseName"
            placeholder="请输入题目名称"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <!-- <el-form-item label="课程编码" prop="courseCode">
          <el-input
            v-model="queryParams.courseCode"
            placeholder="请输入课程编码"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="课程性质" prop="courseNature">
          <el-select
            v-model="queryParams.courseNature"
            placeholder="请选择课程性质"
            clearable
            size="small"
          >
            <el-option
              v-for="dict in courseNatureOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="课程属性" prop="courseAttribute">
          <el-select
            v-model="queryParams.courseAttribute"
            placeholder="请选择课程属性"
            clearable
            size="small"
          >
            <el-option
              v-for="dict in courseAttributeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="课程学分" prop="courseCredit">
          <el-input
            v-model="queryParams.courseCredit"
            placeholder="请输入课程学分"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item> -->
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
        </el-form-item>
      </el-form>

      <!-- <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['org:course:add']"
            >新增</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['org:course:edit']"
            >修改</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['org:course:remove']"
            >删除</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            :loading="exportLoading"
            @click="handleExport"
            v-hasPermi="['org:course:export']"
            >导出</el-button
          >
        </el-col>
        <right-toolbar
          :showSearch.sync="showSearch"
          @queryTable="getList"
        ></right-toolbar>
      </el-row> -->

      <el-table
        v-loading="loading"
        :data="courseList"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <!--      <el-table-column label="课程学分" align="center" prop="id" />-->
        <el-table-column label="试题编号" align="center" prop="id" />
        <el-table-column label="收藏日期" align="center" prop="time" />
        <el-table-column label="题目名称" align="center" prop="title" />
        <el-table-column label="题型内容" align="center" prop="content" />
        <!-- <el-table-column label="操作" align="center" prop="courseCode" /> -->
        <!-- <el-table-column
            label="开课学年"
            align="center"
            prop="schoolYearName"
          />
           <el-table-column
            label="开课学期"
            align="center"
            prop="termName"
          />
           <el-table-column
            label="开课学院"
            align="center"
            prop="collegeName"
          /> -->
        <!-- <el-table-column
          label="课程性质"
          align="center"
          prop="courseNature"
          :formatter="courseNatureFormat"
        />
        <el-table-column
          label="课程属性"
          align="center"
          prop="courseAttribute"
          :formatter="courseAttributeFormat"
        />
        <el-table-column label="课程学分" align="center" prop="courseCredit" /> -->
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-button
              v-if="showSelect"
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['org:course:edit']"
              >取消收藏</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />

      <!-- 添加或修改基本课程管理对话框 -->
      <el-dialog
        :title="title"
        :visible.sync="open"
        width="500px"
        append-to-body
      >
        <!-- 添加课程信息的步骤条 -->
        <!-- <div>
          <el-steps :active="addActive" align-center finish-status="success">
            <el-step title="课程所在组织" description></el-step>
            <el-step title="课程基础信息" description></el-step>
            <el-step title="提交" description></el-step>
          </el-steps>

          <div>
            <div style="width: 50%; margin: 20px auto">{{ addText }}</div> -->
        <el-form ref="form" :model="form" :rules="rules" label-width="100px">
          <!-- <div v-show="addActive===0"> -->
          <!-- <el-form-item label="学年名称" prop="schoolYearId">
                  <el-select
                    v-model="form.schoolYearId"
                    placeholder="请选择学年"
                    clearable
                    size="small"
                    style=" width: 100%;"
                    @change="addTermByYear()"
                  >
                    <el-option
                      v-for="item in yearOptions"
                      :key="item.index"
                      :label="item.name"
                      :value="item.id"
                    />
                  </el-select>
                </el-form-item>
                <el-form-item label="学期名称" prop="termId">
                  <el-select
                    v-model="form.termId"
                    placeholder="请选择学期"
                    clearable
                    size="small"
                    style="width: 100%;"
                    @change="addCollegeByTerm()"
                  >
                    <el-option
                      v-for="item in termNameOptions"
                      :key="item.index"
                      :label="item.termName"
                      :value="item.id"
                    />
                  </el-select>
                </el-form-item>
                <el-form-item label="学院名称" prop="collegeId">
                  <el-select
                    v-model="form.collegeId"
                    placeholder="请选择学院"
                    clearable
                    size="small"
                    style="width: 100%;"
                  >
                    <el-option
                      v-for="item in collegeOptions"
                      :key="item.index"
                      :label="item.name"
                      :value="item.id"
                    />
                  </el-select>
                </el-form-item>
              </div>

              <div v-show="addActive === 1"> -->
          <el-form-item label="开课单位" prop="startingUnit">
            <el-select
              v-model="form.startingUnit"
              placeholder="请选择学院"
              clearable
              filterable
              size="small"
              style="width: 100%"
            >
              <el-option
                v-for="item in coledoptions"
                :key="item.index"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="课程名称" prop="courseName">
            <el-input v-model="form.courseName" placeholder="请输入课程名称" />
          </el-form-item>
          <el-form-item label="课程编码" prop="courseCode">
            <el-input v-model="form.courseCode" placeholder="请输入课程编码" />
          </el-form-item>

          <el-form-item label="课程性质" prop="courseNature">
            <el-select
              v-model="form.courseNature"
              placeholder="请选择课程性质"
              style="width: 100%"
              @change="handleChange('form', 'courseNature')"
            >
              <el-option
                v-for="dict in courseNatureOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="课程属性" prop="courseAttribute">
            <el-select
              v-model="form.courseAttribute"
              placeholder="请选择课程属性"
              style="width: 100%"
              @change="handleChange('form', 'courseAttribute')"
            >
              <el-option
                v-for="dict in courseAttributeOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="课程学分" prop="courseCredit">
            <el-input
              v-model.number="form.courseCredit"
              placeholder="请输入课程学分"
            />
          </el-form-item>
          <!-- </div> -->
        </el-form>
        <!-- </div> -->
        <!-- </div> -->

        <!-- <div class="neper">
            <el-button
              style="margin-top: 12px; margin-right: 10px"
              v-show="addActive > 0"
              @click="addper"
              >上一步</el-button
            >
            <el-button
              style="margin-top: 12px; margin-left: 10px"
              v-show="addActive < 2"
              @click="addnext"
              >下一步</el-button
            >
          </div> -->

        <!-- <span slot="footer" class="dialog-footer" v-show="addActive == 2">
            <el-button @click="cancel">取 消</el-button>
            <el-button type="primary" @click="submitForm(), (addActive = 0)"
              >确 定</el-button
            >
          </span> -->
        <span slot="footer" class="dialog-footer">
          <el-button @click="cancel">取 消</el-button>
          <el-button type="primary" @click="submitForm()">确 定</el-button>
        </span>
      </el-dialog>
      <!-- </el-col> -->
    </el-row>
  </div>
</template>

<script>
import { listCollege, getNotPageList } from "@/api/org/college";
import {
  listCourse,
  getCourse,
  delCourse,
  addCourse,
  updateCourse,
  exportCourse,
} from "@/api/org/course";

import { course } from "@/views/mixins/course/index";
import { validateCode } from "@/utils/formCheck";

export default {
  name: "Course",
  mixins: [course],
  props: {
    showSelect: {
      type: Boolean,
      default: true,
    },
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 基本课程管理表格数据
      courseList: [
        {
          id: "01",
          time: "2022-07-03",
          title: "完形填空",
          content: "听力理解",
        },
        {
          id: "02",
          time: "2022-07-03",
          title: "完形填空",
          content: "听力理解",
        },
      ],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 开课单位字典
      startingUnitOptions: [],
      // 课程性质字典
      courseNatureOptions: [],
      // 课程属性字典
      courseAttributeOptions: [],
      //学院
      coledoptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        schoolYearId: null,
        collegeId: null,
        termId: null,
        id: null,
        startingUnit: null,
        courseName: null,
        courseCode: null,
        startingUnit: null,
        courseNature: null,
        courseAttribute: null,
        courseCredit: null,
      },
      // 表单参数
      form: {
        schoolYearId: null,
        collegeId: null,
        startingUnit: null,
        termId: null,
        courseName: null,
        courseCode: null,
        courseNature: null,
        courseAttribute: null,
        courseCredit: null,
      },
      // 表单校验
      rules: {
        schoolYearId: [
          { required: true, message: "请选择学年", trigger: "blur" },
        ],
        termId: [{ required: true, message: "请选择学期", trigger: "blur" }],
        collegeId: [{ required: true, message: "请选择学院", trigger: "blur" }],
        courseName: [
          { required: true, message: "请输入课程名称", trigger: "blur" },
        ],
        courseCode: [
          { required: true, message: "请输入课程编码", trigger: "blur" },
          { validator: validateCode, trigger: "blur" },
        ],
        courseNature: [
          { required: true, message: "请选择课程性质", trigger: "blur" },
        ],
        courseAttribute: [
          { required: true, message: "请选择课程属性", trigger: "blur" },
        ],
        courseCredit: [
          { required: true, message: "请输入课程学分", trigger: "blur" },
        ],
        startingUnit: [
          { required: true, message: "请选择开课单位", trigger: "blur" },
        ],
      },

      data: [
        {
          label: "信息工程学院",
        },
        {
          label: "法学院",
        },
        {
          label: "土木学院",
        },
        {
          label: "物理学院",
        },
        {
          label: "人文学院",
        },
      ],

      defaultProps: {
        children: "children",
        label: "label",
      },

      // 基本课程
      courseOptions: [
        {
          id: "",
          courseName: "",
        },
      ],
    };
  },
  created() {
    this.total = 2;
    this.loading = false;
    // this.getList();
    // this.getcollList();
    // this.getDicts("starting_unit").then((response) => {
    //   this.startingUnitOptions = response.data;
    // });
    // this.getDicts("course_nature").then((response) => {
    //   this.courseNatureOptions = response.data;
    // });
    // this.getDicts("course_attribute").then((response) => {
    //   this.courseAttributeOptions = response.data;
    // });
  },
  methods: {
    /** 查询学院管理列表 */
    getcollList() {
      this.loading = true;
      getNotPageList(this.queryParams).then((response) => {
        this.coledoptions = response;
        this.loading = false;
      });
    },
    // 选择课程
    selectCourse(row) {
      this.$emit("selectCourse", row);
    },

    resetCourseOptions() {
      this.courseOptions = [
        {
          id: "",
          courseName: "",
        },
      ];
    },
    // 搜索栏通过学院搜索课程
    searchCourseByCollege() {
      this.queryParams.id = null;
      this.resetCourseOptions();
      if (this.queryParams.CollegeId !== "") {
        listCourse(this.queryParams).then((res) => {
          this.courseOptions = res.rows;
        });
      }
    },

    handleNodeClick(data) {
      console.log(data);
    },
    /** 查询基本课程管理列表 */
    getList() {
      this.loading = true;
      listCourse(this.queryParams).then((response) => {
        this.courseList = response.rows;
        console.log("courseList: ", this.courseList);
        this.total = response.total;
        this.loading = false;
      });
    },
    // 开课单位字典翻译
    startingUnitFormat(row, column) {
      return this.selectDictLabel(this.startingUnitOptions, row.startingUnit);
    },
    // 课程性质字典翻译
    courseNatureFormat(row, column) {
      return this.selectDictLabel(this.courseNatureOptions, row.courseNature);
    },
    // 课程属性字典翻译
    courseAttributeFormat(row, column) {
      return this.selectDictLabel(
        this.courseAttributeOptions,
        row.courseAttribute
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        schoolYearId: null,
        collegeId: null,
        termId: null,
        id: null,
        courseName: null,
        courseCode: null,
        startingUnit: null,
        courseNature: null,
        courseAttribute: null,
        courseCredit: null,
      };
      this.resetForm("form");
      this.closedDialog();
    },
    // 用户打开弹出框的，初始化步骤条的信息
    closedDialog() {
      this.addActive = 0;
      this.addText = "以下为课程所在组织的基本信息:";
    },
    // 用户点击添加考试中的下一步按钮
    addnext() {
      this.addActive++;
      if (this.addActive === 0) {
        this.addText = "以下为课程所在组织的基本信息:";
      } else if (this.addActive === 1) {
        this.addText = "以下为课程相关信息:";
      } else if (this.addActive === 2) {
        this.addText = "请提点击确定提交课程信息";
      }
    },
    addper() {
      this.addActive--;
      if (this.addActive === 0) {
        this.addText = "以下为课程所在组织的基本信息:";
      } else if (this.addActive === 1) {
        this.addText = "以下为课程相关信息:";
      } else if (this.addActive === 2) {
        this.addText = "请提点击确定提交课程信息";
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加基本课程管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getCourse(id).then((response) => {
        this.form = response.data;
        console.log("获取的form信息： ", this.form);
        this.open = true;
        this.title = "修改基本课程管理";

        this.termNameOptions[0].id = response.data.termId;
        this.termNameOptions[0].termName = response.data.termName;
        this.collegeOptions[0].id = response.data.collegeId;
        this.collegeOptions[0].name = response.data.collegeName;
      });
    },
    /** 提交按钮 */
    submitForm() {
      // 对整个表单进行校验，如果校验通过则执行传入的回调函数
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateCourse(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            console.log("新增form: ", this.form);
            addCourse(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm(
        '是否确认删除基本课程管理编号为"' + ids + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delCourse(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有基本课程管理数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.exportLoading = true;
          return exportCourse(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
          this.exportLoading = false;
        })
        .catch(() => {});
    },
  },
};
</script>

<style>
.drag-container {
  /*background-color: pink;*/
  position: relative;
}
#drag-content {
  padding: 0 5px;
}

@media screen and (max-width: 768px) {
  #drag-content {
    margin-top: 20px;
  }
}
.datagrid-container {
  margin-top: 20px !important;
}
.drag-area {
  height: 100%;
  min-width: 30px;
  /*background-color: green;*/
  /*border: 1px solid red;*/
  position: relative;
}
.drag-tip {
  height: 100%;
  width: 6px;
  position: absolute;
  top: 0;
  right: 0;
  /* background-color: transparent; */
  background-color: #ccc;
}
/*鼠标移入显示可拖动样式*/
.drag-tip:hover {
  cursor: w-resize;
}
</style>
